import random

from django.http import HttpResponse
from django.shortcuts import render

from s25 import settings
from utils.tencent.sms import send_sms_single

def sms(request):
    """
    url?tpl=register    ---->注册模板id：571781
    :param request:
    :return:
    """

    tpl = request.GET.get("tpl")
    template_id = settings.TENCENT_SMS_TEMPLATE.get(tpl)
    if not template_id:
        return HttpResponse("短信模板不存在")
    # 产生六位数验证码
    code = random.randint(100000,999999)
    # 发送验证码
    res = send_sms_single('18708162392',template_id,[code,])
    print(res)
    if res['result'] == 0:
        return HttpResponse('成功')
    return HttpResponse(res['errmsg'])

from app01 import models
from django import forms
from django.core.validators import RegexValidator
class RegisterModelForm(forms.ModelForm):
    # 这儿有认这儿，这儿没有找模型类
    mobile_phone = forms.CharField(label="手机号",validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'), ])
    password = forms.CharField(label='密码',widget=forms.PasswordInput())
    confirm_password = forms.CharField(label='重复密码',widget=forms.PasswordInput(attrs={"class":"form-control","placeholder":"请重复密码"}))
    code = forms.CharField(label='验证码',widget=forms.TextInput(attrs={"class":"form-control","placeholder":"请输入验证码"}))

    class Meta:
        model = models.UserInfo
        fields = ["username","email","password","confirm_password","mobile_phone","code"]

    # 为了不在每一个字段加{"class":'form-control','placeholder':"请输入。。。"}
    def __init__(self,*args,**kwargs):
        super().__init__(*args,*kwargs)
        # name就是字段名，label就是verbose_name
        for name,field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入{}'.format(field.label)


def register(request):
    form = RegisterModelForm()
    return render(request,"register.html",{'form':form})